package com.tinysand.advert.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

/**
 * Zuul 网关请求的后置过滤器
 *
 * Created By TINY
 * Create Time 2020/2/14
 **/
@Slf4j
@Component // 标识为Spring组件
public class PostRequestFilter extends ZuulFilter {

    /* 过滤器处理的请求类型 */
    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    /* 过滤器的执行优先级（相同类型间）*/
    @Override
    public int filterOrder() {
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1;
    }

    /* 是否执行过滤器 */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /* 业务逻辑方法 */
    @Override
    @SuppressWarnings("all")
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        log.info("请求（{}）结束，花费时间（{}）ms",
                ctx.getRequest().getRequestURI(),
                (long) ctx.get("startTime") - System.currentTimeMillis());

        return null;
    }
}
